package com.symbio.ishenapi.order.controller;

import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.order.bean.TmSubscribeRecordVo;
import com.symbio.ishenapi.order.model.TmAppointmentNumber;
import com.symbio.ishenapi.order.model.TmSubscribeRecord;
import io.jboot.web.controller.annotation.RequestMapping;

import java.util.*;


@RequestMapping("/tmSubscribeRecordController")
public class TmSubscribeRecordController extends BaseController {

    public void findByOrderId(){
        TmSubscribeRecord tmSubscribeRecord =  (TmSubscribeRecord)getReqBodyObject(TmSubscribeRecord.class);
        TmSubscribeRecord tsr = TmSubscribeRecord.dao.getByorderId(tmSubscribeRecord.getOrderId());
        if(tsr != null){
            TmSubscribeRecordVo tmSubscribeRecordVo = new TmSubscribeRecordVo();
            tmSubscribeRecordVo.setId(tsr.getId());
            tmSubscribeRecordVo.setAppointmentDate(tsr.getAppointmentDate());
            tmSubscribeRecordVo.setAppointmentId(tsr.getAppointmentId());
            tmSubscribeRecordVo.setCreateTime(tsr.getCreateTime());
            tmSubscribeRecordVo.setOrderId(tsr.getOrderId());
            tmSubscribeRecordVo.setStatus(tsr.getStatus());

            tmSubscribeRecordVo.setSurplusNumber(null);
            tmSubscribeRecordVo.setTimeSlot(tsr.getTimeSlot());
            tmSubscribeRecordVo.setUpdateUserId(tsr.getUpdateUserId());
            tmSubscribeRecordVo.setUpdateUserName(tsr.getUpdateUserName());
        }
    }


    /**
     * 获取总的余号
     */
    public void findAll(){
        Date date = getParaToDate("appointmentDate");
        TmSubscribeRecord tmSubscribeRecord =  new TmSubscribeRecord();
        tmSubscribeRecord.setAppointmentDate(date);
        String str[] ={"9:00-9:15","9:15-9:30","9:30-9:45","9:45-10:00","10:00-10:15","10:15-10:30","10:30-10:45","10:45-11:00","11:00-11:15",
                "11:15-11:30","11:30-11:45","11:45-12:00","12:00-12:15","12:15-12:30","12:30-12:45","12:45-13:00","13:00-13:15","13:15-13:30",
                "13:30-13:45","13:45-14:00","14:00-14:15","14:15-14:30","14:30-14:45","14:45-15:00","15:00-15:15","15:15-15:30","15:30-15:45",
                "15:45-16:00","16:00-16:15","16:15-16:30","16:30-16:45","16:45-17:00","17:00-17:15","17:15-17:30","17:30-17:4","17:45-18:00",
                "18:00-18:15","18:15-18:30","18:30-18:45","18:45-19:00","19:00-19:15","19:15-19:30","19:30-19:45","19:45-20:00","20:00-20:15",
                "20:15-20:30","20:30-20:45","20:45-21:00"};
        List<TmSubscribeRecord> list = null;
        List<Long> timeSlotList = new ArrayList<>();
        List<String> strList = new ArrayList<>();
        TmAppointmentNumber tmAppointmentNumber = TmAppointmentNumber.dao.getAll();//查询配置表
        if(tmAppointmentNumber != null){
            for(int i = 0;i<str.length;i++){
                tmSubscribeRecord.setTimeSlot(str[i]);
                list = TmSubscribeRecord.dao.getByTimeSlot(tmSubscribeRecord);//根据日期和时间段查询

                if(list == null || list.size() == 0){
                    timeSlotList.add(tmAppointmentNumber.getAppointmentNumber());
                    strList.add(str[i]) ;
                }else{
                    Long number = tmAppointmentNumber.getAppointmentNumber() - list.size();//总次数减去预约的次数
                    if(number > 0){
                        strList.add(str[i]) ;
                        timeSlotList.add(number);
                    }

                }
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("strList",strList);
        map.put("timeSlotList",timeSlotList);
        renderJson(map);
    }


    /**
     *根据日期和时间段返回单个余号
     */
    public void getByTimeSlot(){
        TmSubscribeRecord tmSubscribeRecord =  (TmSubscribeRecord)getReqBodyObject(TmSubscribeRecord.class);
        List<TmSubscribeRecord> list = TmSubscribeRecord.dao.getByTimeSlot(tmSubscribeRecord);
        TmAppointmentNumber tmAppointmentNumber = TmAppointmentNumber.dao.getAll();//查询配置表
        Long num = 0L;
        if(list == null || list.size() == 0){
            num = tmAppointmentNumber.getAppointmentNumber();
        }else{
            num = tmAppointmentNumber.getAppointmentNumber() - list.size();//总次数减去预约的次数
        }
        renderJson(num);
    }
}
